package cn.itsource.web.interceptor;

import cn.itsource.domain.Employee;
import cn.itsource.web.controller.LoginController;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**--------------------------------------------------------
登录检查拦截器
 --------------------------------------------------------**/
@Component
public class LoginCheckInterceptor implements HandlerInterceptor{

    /**--------------------------------------------------------
     方法说明：方法执行前做登录检查
     handler:就是当前要请求的方法
     步骤说明：
     1.从Session中获取Employee
     2.如果为空，没登录，跳转登录页面
     3.如果不为空，放行
     --------------------------------------------------------**/
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {

        //1.从Session中获取Employee
        Employee emp = (Employee) request.getSession().getAttribute(LoginController.EMPLOYEE_IN_SESSION);

        //2.如果为空，没登录，跳转登录页面
        if(null == emp){
            response.sendRedirect("/login.html");
            return false;
        }

        //3.如果不为空，放行
        return true;
    }
}
